package cn.thens.kdroid.sample.launcher

import android.content.Context
import android.content.Intent
import android.content.pm.ResolveInfo
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.thens.kdroid.core.util.dp
import group.infotech.drawable.dsl.shapeDrawable
import group.infotech.drawable.dsl.solidColor
import group.infotech.drawable.dsl.stroke
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit
import kotlin.math.floor

class LauncherMainActivity : AppCompatActivity() {
    private val cellGridAdapter by lazy { AppItemAdapter() }
    private lateinit var vTitle: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(createContentView(this))
        showAppsStuff()
    }

    private fun showAppsStuff() {
        val apps = AppItemAdapter.getAppList(this)
        val list = ArrayList<ResolveInfo>()
        Observable.interval(50, TimeUnit.MILLISECONDS, Schedulers.io())
                .map {
                    val needsAdd = it % (apps.size * 2) < apps.size
                    val randomIndex = floor(Math.random() * list.size).toInt()
                    if (needsAdd) {
                        list.add(randomIndex, apps[(it % apps.size).toInt()])
                    } else {
                        list.removeAt(randomIndex)
                    }
                }
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext { setApps(list) }
                .subscribe()
    }

    private fun createContentView(context: Context): View {
        val wrapContent = ViewGroup.LayoutParams.WRAP_CONTENT
        val matchParent = ViewGroup.LayoutParams.MATCH_PARENT
        return LinearLayout(context).apply {
            orientation = LinearLayout.VERTICAL
            vTitle = TextView(context).apply {
                text = "All apps"
                layoutParams = LinearLayout.LayoutParams(wrapContent, wrapContent).apply {
                    background = shapeDrawable {
                        shape = GradientDrawable.RECTANGLE
                        solidColor = Color.parseColor("#199fff")
                        cornerRadius = dp(4).toFloat()
                        stroke {
                            width = dp(2)
                            color = Color.parseColor("#444444")
                        }
                    }
                }
            }.also { addView(it) }

            RecyclerView(context).apply {
                layoutManager = GridLayoutManager(context, 4)
                adapter = cellGridAdapter
                layoutParams = LinearLayout.LayoutParams(matchParent, matchParent)
            }.also { addView(it) }
        }
    }

    fun setApps(apps: List<ResolveInfo>) {
        cellGridAdapter.refill(apps)
    }

    companion object {
        fun start(context: Context) {
            context.startActivity(Intent(context, LauncherMainActivity::class.java))
        }
    }
}
